package com.thinkgem.jeesite.modules.shop.item.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.shop.item.entity.ShopItem;
import com.thinkgem.jeesite.modules.shop.item.service.IShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by liufeng on 2016/6/3.
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/item")
public class ShopItemController extends BaseController{


    @Autowired
    private IShopItemService shopItemService;


    @RequestMapping(value = {"list", ""})
    public String list(ShopItem item, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ShopItem> page = shopItemService.findShopItem(new Page<ShopItem>(request, response), item);
        model.addAttribute("page", page);
        return "modules/shop/itemList";
    }




    @RequestMapping(value = "form")
    public String form(ShopItem shopItem, Model model) {
        model.addAttribute("shopItem", shopItem);
        return "modules/shop/itemForm";
    }

    @RequestMapping(value = "save")
    public String save(ShopItem shopItem, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, shopItem)){
            return form(shopItem, model);
        }
        // 保存用户信息
        shopItemService.saveItem(shopItem);
        // 清除当前用户缓存
        addMessage(redirectAttributes, "保存用户'" + shopItem.getItemName() + "'成功");
        return "redirect:" + adminPath + "/shop/item/list?repage";
    }

}
